package kxfang.com.login.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;

import kxfang.com.base.activity.MvvmBaseActivity;
import kxfang.com.base.utils.GsonUtils;
import kxfang.com.base.viewmodel.MvmBaseViewModel;
import kxfang.com.common.config.HttpConfig;
import kxfang.com.common.dialog.WaitDialog;
import kxfang.com.common.utils.StatusBarUtil;
import kxfang.com.common.utils.Validator;
import kxfang.com.http.EasyHttp;
import kxfang.com.http.callback.SimpleCallBack;
import kxfang.com.http.exception.ApiException;
import kxfang.com.login.R;
import kxfang.com.login.bean.AppUser;
import kxfang.com.login.bean.MsgModel;
import kxfang.com.login.databinding.LoginActivityPwdBinding;
import kxfang.com.login.parameter.GetCodePar;
import kxfang.com.common.parameter.LoginPar;


public class PwdLoginActivity extends MvvmBaseActivity<LoginActivityPwdBinding, MvmBaseViewModel> {

    GetCodePar getCodePar = new GetCodePar();
    private int type;
    private WaitDialog.Builder waitDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setTopActivityView(this, viewDataBinding.topView);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    protected MvmBaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.login_activity_pwd;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    private void initView() {
        Intent intent = getIntent();
        //type=1  找房者 2===经纪人
        viewDataBinding.loginButton.setOnClickListener(v -> {
            if (!Validator.isMobile(viewDataBinding.phoneEdit.getText().toString().trim())) {
                ToastUtils.showShort("请输入正确的手机号");
                return;
            }
            if (viewDataBinding.codeValue.getText().toString().isEmpty()) {
                ToastUtils.showShort("验证码不能为空");
                return;
            }
            if (viewDataBinding.pwdEdit.getText().toString().isEmpty()) {
                ToastUtils.showShort("密码不能为空");
                return;
            }
            waitDialog = new WaitDialog.Builder(this);
            waitDialog.setMessage("修改密码中...").show();
            login();
        });
        viewDataBinding.getCodeText.setOnClickListener(v -> {
            getCode();
        });
        //关闭界面
        viewDataBinding.deleteActivity.setOnClickListener(v -> {
            finish();
        });
    }

    //获取验证码
    private void getCode() {
        if (!Validator.isMobile(viewDataBinding.phoneEdit.getText().toString().trim())) {
            ToastUtils.showShort("请输入正确的手机号码");
            return;
        }
        getCodePar.setPhone(viewDataBinding.phoneEdit.getText().toString());
        getCodePar.setVeriCode(3);
        timer.start();
        loadCode(getCodePar);
    }

    CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            viewDataBinding.getCodeText.setEnabled(false);
            viewDataBinding.getCodeText.setText("已发送(" + millisUntilFinished / 1000 + ")");
        }

        @Override
        public void onFinish() {
            viewDataBinding.getCodeText.setEnabled(true);
            viewDataBinding.getCodeText.setText("获取验证码");

        }
    };

    //请求验证码
    private void loadCode(GetCodePar par) {
        EasyHttp.post(HttpConfig.loadCode)
                .cacheKey(HttpConfig.loadCode.concat(PwdLoginActivity.class.getSimpleName()))
                .upJson(GsonUtils.toJson(par))
                .execute(new SimpleCallBack<MsgModel>() {
                    @Override
                    public void onSuccess(MsgModel msgModel) {
                        if (msgModel.getCode() != 200) {
                            ToastUtils.showShort(msgModel.getMsg());
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onCompleted() {

                    }
                });

    }

    private void login() {
        LoginPar par = new LoginPar();
        par.setPhone(viewDataBinding.phoneEdit.getText().toString());
        par.setYzm(viewDataBinding.codeValue.getText().toString());
        par.setPwd(viewDataBinding.pwdEdit.getText().toString());
        par.setPersonType(3);
        EasyHttp.post(HttpConfig.login)
                .cacheKey(HttpConfig.login.concat(PwdLoginActivity.class.getSimpleName()))
                .upJson(GsonUtils.toJson(par))
                .execute(new SimpleCallBack<AppUser>() {
                    @Override
                    public void onSuccess(AppUser appUser) {
                        ToastUtils.showShort("密码修改成功");
                        finish();
                    }

                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onCompleted() {
                        waitDialog.dismiss();
                    }
                });

    }

}
